echo 输入的渠道号参数="$1"
echo 输入的版本号参数="$2"
echo 输入的VersionCode参数="$3"
echo 输入的Abi参数="$4"
echo 输入的应用名称参数="$5"
echo 输入的env参数="$6"
echo 输入的flavor参数="$7"
echo 输入的adInitStartTime参数="$8"
echo 输入的adInitEndTime参数="${9}"
echo 输入的oaidStartTime参数="${10}"
echo 输入的oaidEndTime参数="${11}"
echo 输入的junkCodeLink参数="${12}"
OLD_IFS="$IFS"
IFS=","
arr=($1)
versionArr=($2)
versionCodeArr=($3)
abiArr=($4)
nameArr=($5)
envArr=($6)
envParam="true"
flavorArr=($7)
adInitStartTime=(${8:-})
adInitEndTime=(${9:-})
oaidStartTime=(${10:-})
oaidEndTime=(${11:-})
junkCodeLink=(${12:-})
length=${#arr[@]}

# 测试 sed 命令可用性
echo "测试 sed 命令..."
sed --version || gsed --version || {
    echo "错误：未找到 sed 或 gsed 命令"
    exit 1
}

# 新增部分：校对 build.gradle 的 Maven 仓库逻辑
BUILD_GRADLE="./android/build.gradle"
# 验证路径
if [ ! -f "$BUILD_GRADLE" ]; then
    echo "错误：build.gradle 路径无效，当前目录：$(pwd)"
    exit 1
fi
REPOSITORY_PANGLE="https://artifact.bytedance.com/repository/pangle"
REPOSITORY_VOLCENGINE="https://artifact.bytedance.com/repository/Volcengine/"

# 检查并添加 pangle 仓库（兼容 macOS 和 Linux）
if ! grep -q "$REPOSITORY_PANGLE" "$BUILD_GRADLE"; then
    echo "添加 pangle 仓库到 build.gradle"
    # macOS 和 Linux 兼容写法
    sed -i.bak '/allprojects {/,/}/ {
        /repositories {/a \
        maven{url "'"$REPOSITORY_PANGLE"'"}
    }' "$BUILD_GRADLE" && rm -f "$BUILD_GRADLE.bak"
fi

# 检查并添加 Volcengine 仓库
if ! grep -q "$REPOSITORY_VOLCENGINE" "$BUILD_GRADLE"; then
    echo "添加 Volcengine 仓库到 build.gradle"
    sed -i.bak '/allprojects {/,/}/ {
        /repositories {/a \
        maven{url "'"$REPOSITORY_VOLCENGINE"'"}
    }' "$BUILD_GRADLE" && rm -f "$BUILD_GRADLE.bak"
fi

# 使用前检查数组是否为空
if [ ${#adInitStartTime[@]} -eq 0 ]; then
    adInitStartTime=("")  # 设为包含一个空字符串的数组
fi
if [ ${#adInitEndTime[@]} -eq 0 ]; then
    adInitEndTime=("")  # 设为包含一个空字符串的数组
fi
if [ ${#oaidStartTime[@]} -eq 0 ]; then
    oaidStartTime=("")  # 设为包含一个空字符串的数组
fi
if [ ${#oaidEndTime[@]} -eq 0 ]; then
    oaidEndTime=("")  # 设为包含一个空字符串的数组
fi

echo "junkCodeLink：$junkCodeLink"
echo "from remote 3.19.0 数组长度为：$length"
IFS="$OLD_IFS"
# 设置 PUB_HOSTED_URL
export PUB_HOSTED_URL="https://pub.flutter-io.cn"
# 设置 FLUTTER_STORAGE_BASE_URL
export FLUTTER_STORAGE_BASE_URL="https://storage.flutter-io.cn"
#export PUB_HOSTED_URL="https://pub.dev"
#export FLUTTER_STORAGE_BASE_URL="https://storage.googleapis.com"
##循环执行build脚本
# shellcheck disable=SC2068
#下载混淆代码
if [ -z "${12}" ] || [ ${#junkCodeLink[@]} -eq 0  ]; then
    echo "变量为空"
    PROJECT_DIR=$(dirname "$0")
    targetPath="$PROJECT_DIR/android/app/libs/"
    junkFile="${targetPath}junk-release.aar"
    echo ${targetPath}
    if [ -f "${junkFile}" ]; then
      echo "发现旧的混淆代码文件，正在删除..."
      rm -f "${junkFile}"
    fi
else
    PROJECT_DIR=$(dirname "$0")
    targetPath="$PROJECT_DIR/android/app/libs/"
    echo ${targetPath}
    if [ ! -d ${targetPath} ]; then
      echo "混淆代码文件夹不存在"
      mkdir ${targetPath}
      wget -O "${targetPath}junk-release.aar" ${junkCodeLink}
    else
      wget -O "${targetPath}junk-release.aar" ${junkCodeLink}
      echo "混淆代码文件夹已存在"
    fi
fi
for index in $(seq 1 "$length")
do
  apkPath=./output/"${nameArr[index-1]}"
  #创建文件夹
  if [ ! -d "${apkPath}" ]; then
      # shellcheck disable=SC2086
      mkdir -p ${apkPath}
  else
    echo "文件夹已存在"
  fi
  if [[ "${envArr[index-1]}" =~ "Release" ]]; then
    envParam="true"
  else
    # shellcheck disable=SC1068
    envParam="false"
  fi
  rm -rf pubspec.lock
  /root/flutter/flutter3190/flutter/bin/flutter clean
  /root/flutter/flutter3190/flutter/bin/flutter build apk --flavor="${flavorArr[index-1]}${abiArr[index-1]}" --dart-define=APP_CHANNEL="${arr[index-1]}" --dart-define=NAME="${nameArr[index-1]}" --dart-define=IS_PRODUCT="${envParam}" --dart-define=AD_INIT_START_TIME="${adInitStartTime[index-1]}" --dart-define=AD_INIT_END_TIME="${adInitEndTime[index-1]}" --dart-define=OAID_START_TIME="${oaidStartTime[index-1]}" --dart-define=OAID_END_TIME="${oaidEndTime[index-1]}" --build-name="${versionArr[index-1]}" --build-number="${versionCodeArr[index-1]}" --release
  sleep 5s
    cp -R build/app/outputs/apk/"${flavorArr[index-1]}${abiArr[index-1]^}"/release/*.apk "${apkPath}/${versionArr[index-1]}-${arr[index-1]}-${flavorArr[index-1]}${abiArr[index-1]}-release.apk"
done

echo 构建完成
echo apk输出路径:"${apkPath}"
# shellcheck disable=SC2162
#read -n 1